﻿using RPCServer.Attributes;
using RPCServer.Network;
using RPCServer.Tool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RPCServer.Core
{
    class Setting
    {
        public readonly static string RootPath = AppDomain.CurrentDomain.BaseDirectory;

        public readonly static string Suffix = ".setting";

        static Dictionary<string, Dictionary<string, string>> _setting = new();

        static Dictionary<string, object> _loadedSetting = new();

        public Setting()
        {
            InitSetting();
        }

        public static string PathFormat(string fileName)
        {
            return $"{RootPath}{fileName}{Suffix}";
        }

        static void InitSetting()
        {
            foreach (var filePath in CommolOption.GetAllFilePath(RootPath, Suffix))
            {
                Dictionary<string, string> item = new();
                _setting.Add(CommolOption.GetFilePathName(filePath), item);
                KeyValueFileHelper.TryLoad(filePath, ref item);
            }
        }

        public static T GetSetting<T>() where T : class
        {
            Type type = typeof(T);
            SettingAttribute attribute = CommolOption.GetAttribute<SettingAttribute>(type);
            if (attribute == default)
                return default;
            string name = type.Name;
            if (_loadedSetting.ContainsKey(name))
            {
                return _loadedSetting[name] as T;
            }
            //TODO
            return default;
        }
    }
}
